package com.qst.controller;

import com.qst.pojo.Department;
import com.qst.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author KungSu.
 * @create 2021-06-17 10:53
 */
@Controller
public class DepartmentController {
    @Autowired
    DepartmentService departmentService;

    @RequestMapping("/department")
    public String department(Model model) {
        List<Department> departments = departmentService.selectDep();
        model.addAttribute("dep", departments);
        return "departments";
    }

    @RequestMapping("/deleteDep")
    public String delDep(Model model, int departmentid) {
        if(departmentService.delDep(departmentid) == false) {
            model.addAttribute("error", "该部门仍存在员工，请先调整该部门员工");
        }
        return this.department(model);
    }

    @PostMapping("/insertDep")
    public String insertDep(Model model, String departmentname) {
        departmentService.insertDep(departmentname);
        return this.department(model);
    }
}
